// WPPicture.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include <afxdllx.h>
#include "piclibrary.h"
#include "settingsdialog.h"
#include <afxmt.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static AFX_EXTENSION_MODULE WPPictureDLL = { NULL, NULL };


static CPicLibrary m_piclib;

static CCriticalSection m_section;

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	// Remove this if you use lpReserved
	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("WPPICTURE.DLL Initializing!\n");
		
		// Extension DLL one-time initialization
		if (!AfxInitExtensionModule(WPPictureDLL, hInstance))
			return 0;

		// Insert this DLL into the resource chain
		// NOTE: If this Extension DLL is being implicitly linked to by
		//  an MFC Regular DLL (such as an ActiveX Control)
		//  instead of an MFC application, then you will want to
		//  remove this line from DllMain and put it in a separate
		//  function exported from this Extension DLL.  The Regular DLL
		//  that uses this Extension DLL should then explicitly call that
		//  function to initialize this Extension DLL.  Otherwise,
		//  the CDynLinkLibrary object will not be attached to the
		//  Regular DLL's resource chain, and serious problems will
		//  result.

		new CDynLinkLibrary(WPPictureDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("WPPICTURE.DLL Terminating!\n");
		// Terminate the library before destructors are called
		AfxTermExtensionModule(WPPictureDLL);
	}
	return 1;   // ok
}


CString Str(int i)
{	CString S;
	S.Format("%i",i);
	return S;
}


typedef void    ( WINAPI*  SetVarProc) (CString Var,CString Value,void* pElement);
typedef CString ( WINAPI*  GetVarProc) (CString Var,void* pElement);


#define GETVAR_S(VAR)   ((GetVarProc)pGetVarProc)(VAR,Element)
#define GETVAR_I(VAR)   atoi(((GetVarProc)pGetVarProc)(VAR,Element))
#define SETVAR_S(VAR,VALUE)   ((SetVarProc)pSetVarProc)(VAR,VALUE,Element)
#define SETVAR_I(VAR,VALUE)   ((SetVarProc)pSetVarProc)(VAR,Str(VALUE),Element)






//---------------------------------------------------------------------------------
CString LoadString(UINT id)
{
	CString S;
	S.LoadString(id);
	return S;
}



int AFX_EXT_API CALLBACK SettingsDialog(void* Element,void* pSetVarProc,void* pGetVarProc) 
{

	CSettingsDialog dlg;

	dlg.m_filename=GETVAR_S("PicFile");
	dlg.m_isstretch=GETVAR_I("IsStretch");
	dlg.m_ischain=GETVAR_I("IsChain");
	dlg.m_istile=GETVAR_I("IsTile");

	int rc=dlg.DoModal();
	
	if (rc==IDOK) 
	{	m_piclib.UnloadPicture(GETVAR_S("PicFile"));
		SETVAR_S("PicFile",dlg.m_filename);
		SETVAR_I("IsStretch",dlg.m_isstretch);
		SETVAR_I("IsChain",dlg.m_ischain);
		SETVAR_I("IsTile",dlg.m_istile);
		m_piclib.LoadPicture(GETVAR_S("PicFile"));
	}


	return 0;

}




void AFX_EXT_API CALLBACK About(void* Element,void* pSetVarProc,void* pGetVarProc) 
{	
	CString S=GETVAR_S("PicFile");
	if (S=="") S=LoadString(IDS_ERRNOTSET);
	AfxMessageBox(LoadString(IDS_PICTURE)+" v1.0\n"+LoadString(IDS_ABOUT2)+" (c)\n\n"+S,MB_ICONINFORMATION|MB_OK  ,NULL);

}


void AFX_EXT_API CALLBACK Init(void* Element,void* pSetVarProc,void* pGetVarProc) 
{


	SETVAR_S("Descr",LoadString(IDS_PICFROMFILE));
	SETVAR_S("PicFile","");
	SETVAR_I("IsStretch",1);
	SETVAR_I("IsChain",1);
	SETVAR_I("IsTile",0);
	

}




BOOL AFX_EXT_API CALLBACK Paint(CDC* pdc,CRect* ppos,void* Element,void* pSetVarProc,void* pGetVarProc) 
{	
	m_section.Lock();
	m_piclib.Paint(pdc,ppos,GETVAR_S("PicFile"),GETVAR_I("IsStretch"),GETVAR_I("IsChain"),GETVAR_I("IsTile"));

	CString S=GETVAR_S("PicFile");
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];
	_splitpath( S.GetBuffer(1), drive, dir, fname, ext );
	SETVAR_S("Descr",CString(LoadString(IDS_PICTURE)+" (")+fname+ext+")");


	m_section.Unlock();
	return TRUE;
	
}
 

//---------------------------------------------------------------------------


